package com.cskaoyan.service.impl;

import com.cskaoyan.bean.pojo.*;
import com.cskaoyan.bean.vo.SearchIndexVo;
import com.cskaoyan.mapper.MarketKeywordMapper;
import com.cskaoyan.mapper.MarketSearchHistoryMapper;
import com.cskaoyan.service.SearchService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author :LuoXuXin
 * @version : $version
 * @date :Created in 2022/4/8   22:10
 * @description:
 * @return:
 */
@Service
public class SearchServiceImpl implements SearchService {
    @Autowired
    MarketKeywordMapper marketKeywordMapper;
    @Autowired
    MarketSearchHistoryMapper marketSearchHistoryMapper;

    @Override
    public SearchIndexVo searchIndex() {
        SearchIndexVo searchIndexVo = new SearchIndexVo();


        //搜索一个默认关键字（第一个参数）
        MarketKeywordExample marketKeywordExample = new MarketKeywordExample();
        MarketKeywordExample.Criteria criteria = marketKeywordExample.createCriteria();
        criteria.andIsDefaultEqualTo(true);
        List<MarketKeyword> marketKeywords = marketKeywordMapper.selectByExample(marketKeywordExample);
        searchIndexVo.setDefaultKeyword(marketKeywords.get(0));
        //搜索热门关键词，第二个参数
        MarketKeywordExample marketKeywordExample1 = new MarketKeywordExample();
        MarketKeywordExample.Criteria criteria1 = marketKeywordExample1.createCriteria();
        criteria1.andIsHotEqualTo(true);
        List<MarketKeyword> marketKeywords1 = marketKeywordMapper.selectByExample(marketKeywordExample1);
        searchIndexVo.setHotKeywordList(marketKeywords1);

        //第三个参数，用户历史搜索记录
        Subject subject = SecurityUtils.getSubject();
        List<SearchIndexVo.SearchIndexVoInner> searchInner = new ArrayList<>();
        MarketUser principal = (MarketUser) subject.getPrincipal();

            int userId = principal.getId();
            MarketSearchHistoryExample historyExample = new MarketSearchHistoryExample();
            MarketSearchHistoryExample.Criteria criteria2 = historyExample.createCriteria();
        criteria2.andDeletedEqualTo(false);
            criteria2.andUserIdEqualTo(userId);
        historyExample.setOrderByClause("add_time desc");
            List<MarketSearchHistory> searchHistories =
                    marketSearchHistoryMapper.selectByExample(historyExample);

            for (MarketSearchHistory searchHistory : searchHistories) {
                String keyword = searchHistory.getKeyword();
                SearchIndexVo.SearchIndexVoInner indexVoInner = new SearchIndexVo().new SearchIndexVoInner(keyword);
                searchInner.add(indexVoInner);
            }

        searchIndexVo.setHistoryKeywordList(searchInner);

        return searchIndexVo;
    }

    @Override
    public List<String> helperSearch(String keyword) {
        MarketKeywordExample marketKeywordExample = new MarketKeywordExample();
        MarketKeywordExample.Criteria criteria = marketKeywordExample.createCriteria();
        //通配关键字
        criteria.andKeywordLike("%" +keyword +"%");
        List<MarketKeyword> marketKeywords = marketKeywordMapper.selectByExample(marketKeywordExample);
        List<String> list = new ArrayList<>();
        //把结果的keyword字段提取出来
        for (MarketKeyword marketKeyword : marketKeywords) {
            String keyword1 = marketKeyword.getKeyword();
            list.add(keyword1);
        }
        return list;
    }

    @Override
    public void clearHistory() {
        //获得用户信息
        Subject subject = SecurityUtils.getSubject();
        MarketUser principal = (MarketUser) subject.getPrincipal();
        Integer userId = principal.getId();
        //修改该用户历史记录的deleted number为true
        MarketSearchHistoryExample historyExample = new MarketSearchHistoryExample();
        MarketSearchHistoryExample.Criteria criteria = historyExample.createCriteria();
        criteria.andUserIdEqualTo(userId);
        List<MarketSearchHistory> histories = marketSearchHistoryMapper.selectByExample(historyExample);
        for (MarketSearchHistory history : histories) {
            history.setDeleted(true);
            marketSearchHistoryMapper.updateByPrimaryKey(history);
        }
    }
}
